<template>
  <div class="inventoryPosting">
    <div class="header">
      <div class="header-name">
        <el-form :model="proVal" label-width="100px" size="mini">
          <el-form-item :label="$t('market.stareData')">
            <el-date-picker
              v-model="proVal.fdate"
              type="month"
              value-format="yyyy-MM"
              style="width: 175px"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
            <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
          </el-form-item>
        </el-form>
        <!--        <filtration :ftype="ftype" :msg="tacitly" :select-data="selectData" :table-header="tableHeader1" @seachData="handleQuerySonClass" />-->
      </div>
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
        sortable
        :get-summaries="getSummaries"
        show-summary
      >
        <template v-slot:tagSlot="col">
          <el-link type="primary" @click="handleInto(col.scope.row.物料编码)">
            {{ col.scope.row.物料编码 }}
          </el-link>
        </template>
      </jc-table>
    </div>
  </div>
</template>

<script>
import {
  queryDepartment
} from '@/api/financial/salesMargin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import searData from '@/components/Search/mixin'

export default {
  mixins: [searData],
  data() {
    return {
      proVal: {
        fdate: ''
      },
      tableHeight: '73vh',
      // 表头
      tableData: [], // 销售数据
      tableHeader: [
        { label: this.$t('market.subject1'), prop: 'subjectName', width: '120' },
        { label: this.$t('market.totalMonth'), prop: '合计本月', width: '90' },
        { label: this.$t('market.totalYear'), prop: '合计本年', width: '90' }
      ],
      tableHeader1: []
    }
  },
  created() {
    // this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 220px)' : '73vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          const list = this.tableData
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '费用报表')
        }
      })
    },
    // 获取成品配套入库表数据
    async handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        ...this.proVal,
        pageNum: this.currentPage,
        pageSize: this.size
      }
      const { data: RES } = await queryDepartment(DATA)
      this.tableData = RES.detail
      const dis = ['subjectId', 'amountOut', 'amountIn', 'subjectName', 'numberCode', '合计本月', '合计本年']
      this.tableHeader.splice(3)
      RES.data.forEach(res => {
        if (!dis.includes(res)) {
          this.tableHeader.push({
            label: res,
            prop: res
          })
        }
      })
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    handleInto(ev) {
      this.$router.push({ path: `/detailMateriel/${ev}` })
    }
  }
}
</script>
<style lang="scss" scoped>
.inventoryPosting {
  //@include listBom;
  padding: 0;
  .header{
    position:relative;
    .header-name{
      width: 100%;
      span {
        width: 100px;
        font-size: 12px;
        text-align: center;
      }
    }
    .el-form {
      display: flex;
      flex-wrap: wrap;

      .el-form-item {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        width: 275px;
        margin-bottom: 5px;
      }
    }
    .btn{
      transform: translateY(18%);
      margin-left: 15px !important;
    }
  }
}
</style>
